package base.day04;

import java.util.Arrays;

/**
 * 描述：
 *      "之"子型打印矩阵
 * @author hl
 * @version 1.0
 * @date 2020/10/26 16:22
 */
public class Code_04_ZigZagPrintMatrix {
    public static void printMatrixZiaZig(int[][] matrix){
        int tR = 0;
        int tC = 0;
        int dR = 0;
        int dC = 0;
        int endR = matrix.length - 1;
        int endC = matrix[0].length - 1;
        boolean fromUp = false;
        while(tR != endR + 1){
            printLevel(matrix, tR, tC, dR, dC,fromUp);
            //顺序不能乱
            tR = tC == endC ? tR + 1 : tR;//先判断上一个dC的情况，决定此次tR
            tC = tC == endC ? tC : tC + 1;
            dC = dR == endR ? dC + 1 : dC;//先判断上一个dR的情况，决定此次dC
            dR = dR == endR ? dR : dR + 1;
            fromUp = !fromUp;
        }
    }

    public static void printLevel(int[][] matrix, int tR, int tC, int dR, int dC, boolean fromUp){
        if(fromUp){
            while(tR <= dR){
                System.out.print(matrix[tR++][tC--] + " ");
            }
        }else{
            while(tR <= dR){
                System.out.print(matrix[dR--][dC++] + " ");
            }
        }
    }
    public static void main(String[] args) {
        int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
        for (int[] ints : matrix) {
            System.out.println(Arrays.toString(ints));
        }
        printMatrixZiaZig(matrix);

    }

}
